﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Aiemp.ObjectModel;

namespace RaisingForce.Raise.Presentation.Localization
{
    sealed public class SupportedLocalizationLanguages :
        Singleton<SupportedLocalizationLanguages>,
        IEnumerable<LocalizationLanguage>
    {
        #region Constructors

        private SupportedLocalizationLanguages()
        { }

        #endregion

        #region Private Declarations

        private readonly ICollection<LocalizationLanguage> supportedLanguages =
            new ObjectCollection<LocalizationLanguage>();

        #endregion

        #region Properties

        public int Count
        {
            get
            {
                Contract.Ensures(Contract.Result<int>() >= 0);
                Contract.Assume(this.supportedLanguages.Count >= 0);
                return this.supportedLanguages.Count;
            }
        }

        #endregion

        #region Public Collection Methods

        public void AddLanguage(LocalizationLanguage language)
        {
            if (language == null)
            {
                throw new ArgumentNullException("language");
            }
            Contract.Ensures(this.Count == Contract.OldValue(this.Count) + 1);
            Contract.EndContractBlock();
            lock (this.supportedLanguages)
            {
                this.supportedLanguages.Add(language);
            }
        }

        public void AddMultipleLanguages(
            IEnumerable<LocalizationLanguage> languages)
        {
            if (languages == null)
            {
                throw new ArgumentNullException("languages");
            }
            Contract.Ensures(this.Count >= Contract.OldValue(this.Count));
            Contract.EndContractBlock();
            lock (this.supportedLanguages)
            {
                foreach (LocalizationLanguage language in languages)
                {
                    this.supportedLanguages.Add(language);
                }
            }
        }

        public void ResetLanguages()
        {
            Contract.Ensures(this.Count == 0);
            if (this.supportedLanguages.Count > 0)
            {
                lock (this.supportedLanguages)
                {
                    this.supportedLanguages.Clear();
                    Contract.Assume(this.supportedLanguages.Count == 0);
                }
            }
        }

        [Pure]
        public bool ContainsLanguage(LocalizationLanguage language)
        {
            lock (this.supportedLanguages)
            {
                return this.supportedLanguages.Contains(language);
            }
        }

        [Pure]
        public bool ContainsLanguage(string cultureName)
        {
            lock (this.supportedLanguages)
            {
                return this.supportedLanguages
                    .Any(language => language.CultureName == cultureName);
            }
        }

        #endregion

        #region IEnumerable<LocalizationLanguage> Members

        public IEnumerator<LocalizationLanguage> GetEnumerator()
        {
            return this.supportedLanguages.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
